
// 获取mongoose
const mongoose = require("mongoose");



const get = async ({
    curPage,
    eachPage
}) => {
    // 强制转换类型 number转string
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    }
    //mongoose.model传三个参数是创建  传一个参数是获取
    const seckillModel = mongoose.model("seckill");
    //get 列表数据
    // 获取当前model映射的那个集合里存在的文档总数
    data.total = await seckillModel.countDocuments();
    // Math.ceil往上取整
    data.maxPage = Math.ceil(data.total / eachPage);
    // find查询所有数据 findOne查询满意结果的第一条
    // skip跳过多少条开始查找 
    //limit 查询多少条数据
    // sort 排序
    //exec 立即执行
    const skipNum = (curPage - 1) * eachPage;
    data.rows = await seckillModel.find().populate('commodity_id').skip(skipNum).limit(eachPage).exec()
    return data;
};

// 新增活动商品
const addOne = async (data) => {




    const { commodity_id, name, Price, restricted,
        Total, Sold, Start_time, End_time, Sale_type, State } = data

    //mongoose.model
    const seckillModel = mongoose.model("seckill");
    //get 列表数据
    const result = await seckillModel.create({
        commodity_id, name, Price, restricted,
        Total, Sold, Start_time, End_time, Sale_type, State
    })
    return result
}
// 修改商品在售状态商品
const changeState = async (data) => {
    const { _id, State } = data
    //mongoose.model
    const seckillModel = mongoose.model("seckill");
    //get 列表数据
    const result = await seckillModel.updateOne({ _id }, { State })
    return result
}

const updataOneSeckill = async (data) => {

    const { commodity_id, name, Price, restricted, _id,
        Total, Sold, Start_time, End_time, Sale_type, State } = data

    const seckillModel = mongoose.model("seckill");
    return await seckillModel.updateOne({ _id }, {
        commodity_id, name, Price, restricted, Start_time, End_time,
        Total, Sold, Sale_type, State
    })

}
const selectSEckills = async ({ name, Start_time }) => {
    const data = {
        rows: [],

    }
    var info = {}
    name ? info.name = { $regex: name, $options: 'i' } : ""
    Start_time ? info.Start_time = { $regex: Start_time, $options: 'i' } : ""
    if (Object.keys(info).length == 0) return data
    //mongoose.model传三个参数是创建  传一个参数是获取
    const seckillModel = mongoose.model("seckill");
    data.rows = await seckillModel.find(info).populate("commodity_id").exec()
    return data;

}

const selectFromToType = async ({ Father_id }) => {

    //商品子类型
    const childTypeModel = mongoose.model("childType")
    // 商品集合
    const commodityModel = mongoose.model("commodity");
    // 活动集合
    const seckillModel = mongoose.model("seckill");
    // 通过夫类型找到子类型的数据
    const childInfo = await childTypeModel.find({ Father_id })
    var data = []//所有商品
    const comData = []//商品信息
    const chilarr = []//子类型
    // 判断找到的子类型存不存在
    if (childInfo.length === 0) {
        return rows = data
        // 存在的话再通过 子类型去找对应商品的数据
    } else {
        childInfo.map(item => {
            chilarr.push(item._id)
        })
        // 通过子集合类型找 商品     Child_type: { "$in": chilarr } 数组里只要有任何一个值与Child_type这个字段匹配则返回
        const cominfo = await commodityModel.find({ Child_type: { "$in": chilarr } })
        // 判断商品存不存在
        if (cominfo.length != 0) {
            cominfo.map(item => {
                comData.push(item._id)
            })
        }
        return await seckillModel.find({ commodity_id: { "$in": comData } }).populate("commodity_id")

    }


}
const selectFromchildType = async ({ child_id }) => {
    // 商品集合
    const commodityModel = mongoose.model("commodity");
    // 活动集合
    const seckillModel = mongoose.model("seckill");
    const comData = []//商品信息
    // 通过子集合类型找 商品
    const cominfo = await commodityModel.find({ Child_type: child_id })
    // 判断商品存不存在
    if (cominfo.length != 0) {
        cominfo.map(item => {
            comData.push(item._id)
        })
        return await seckillModel.find({ commodity_id: { "$in": comData } }).populate("commodity_id")

    } else {
        return []
    }

}
const delSckill = async ({ _id }) => {
    // 活动集合
    const seckillModel = mongoose.model("seckill");
    return seckillModel.deleteOne({ _id })
}


const findOne = async ({ _id }) => {
    // 活动集合
    const seckillModel = mongoose.model("seckill");
    return seckillModel.findOne({ _id }).populate("commodity_id")
}

const selectSEckillsAndPage = async ({ name, Start_time, curPage = 1, eachPage = 5 }) => {
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        rows: [],
        curPage,
        eachPage,
    }
    var info = {}
    name ? info.name = { $regex: name, $options: 'i' } : ""
    Start_time ? info.Start_time = { $regex: Start_time, $options: 'i' } : ""
    if (Object.keys(info).length == 0) return data

    //mongoose.model传三个参数是创建  传一个参数是获取
    const seckillModel = mongoose.model("seckill");
    // Math.ceil往上取整
    // find查询所有数据 findOne查询满意结果的第一条
    // skip跳过多少条开始查找 
    //limit 查询多少条数据
    // sort 排序
    //exec 立即执行
    const skipNum = (curPage - 1) * eachPage;
    // seckillModel.find().populate('commodity_id').skip(skipNum).limit(eachPage).exec()
    data.total = (await seckillModel.find(info)).length

    data.rows = await seckillModel.find(info).skip(skipNum).limit(eachPage).populate("commodity_id").exec()
    data.maxPage = Math.ceil(data.total / eachPage);
    return data

}


module.exports = {

    findOne, get, addOne, changeState, updataOneSeckill, selectSEckills, selectFromToType, selectFromchildType, delSckill, selectSEckillsAndPage

}
